package models

import (
	"strconv"
	"time"
)

type Assets struct {
	*Model
	Name        string `json:"name"`
	Target      string `json:"target"`
	CreatedTime string `json:"created_time"`
	UpdatedTime string `json:"updated_time"`
}

//data := make(map[string]interface{})
//nowTime := time.Now().Format("20060102150405")
//data["name"] = "唐小风"
//data["target"] = "txf.com"
//
//data["created_time"] = nowTime
//data["updated_time"] = nowTime
//models.AddAsset(data)

//新增厂商+target资产
func AddAsset(data map[string]interface{}) {
	assets := Assets{
		Name:        data["name"].(string),
		Target:      data["target"].(string),
		CreatedTime: time.Now().Format("2006-01-02 15:04:05"),
		UpdatedTime: time.Now().Format("2006-01-02 15:04:05"),
	}
	db.AutoMigrate(&assets)
	db.Create(&assets)
}

//查询所有target资产
func GetAllAsset() (assets []Assets) {
	db.Select("target").Find(&assets)
	return
}

//查询数据库中是否存在资产
func ExistAsset(target,name string) (bool, int) {
	var assets Assets
	db.Select("id").Where("target = ? AND name = ?", target,name).First(&assets)
	if assets.ID > 0 {
		return true, assets.ID
	}
	return false, assets.ID
}


//更新资产插入时间
func EditAsset(id int, data interface{}) bool {
	db.Model(&Assets{}).Where("id = ?", id).Updates(data)
	return true
}

//返回自定义的告警端口

func GetPortResult() (iplist []Iplist) {
	dbTmp := db
	dbTmp.Where("port = ? OR port = ? OR port = ? OR port = ? OR port = ? OR port = ? OR port = ? OR port = ? OR port = ? OR port = ? OR service = ? OR service = ? OR service = ? OR service = ? OR service = ? OR service = ? OR service = ? OR service = ? OR service = ? OR service = ? OR service = ? OR service = ? OR service = ? OR res_result = ?", 3389, 22, 1988,21, 3306, 6379, 5200, 446, 7799, 33033,"mysql", "mssql", "redis", "memcache", "mongo", "ftp", "tftp", "ssh", "weblogic", "websphere", "tomcat", "ms-wbt-server", "oracle","[Tuya Application]").Where("status = ?", "open").Find(&iplist)
	return
}

//查看资产

func GetAssets(pageNum int, pageSize int, maps interface{}) (assets []Assets) {
	dbTmp := db
	querys := maps.(map[string]interface{})

	if querys["name"] != nil {
		dbTmp = dbTmp.Where("name LIKE ?", "%"+querys["name"].(string)+"%")
	}


	if querys["target"] != nil {
		dbTmp = dbTmp.Where("target LIKE ?", "%"+querys["target"].(string)+"%")
	}

	if querys["created_time"] != nil {
		dbTmp = dbTmp.Where("created_time LIKE ?", "%"+querys["created_time"].(string)+"%")
	}

	if querys["updated_time"] != nil {
		dbTmp = dbTmp.Where("updated_time LIKE ?", "%"+querys["updated_time"].(string)+"%")
	}

	dbTmp.Offset(pageNum).Limit(pageSize).Order("id  desc").Find(&assets)
	return
}

func GetAssetsTotal(maps interface{}) (count int) {
	dbTmp := db
	querys := maps.(map[string]interface{})

	if querys["name"] != nil {
		dbTmp = dbTmp.Where("name LIKE ?", "%"+querys["name"].(string)+"%")
	}

	if querys["target"] != nil {
		dbTmp = dbTmp.Where("target LIKE ?", "%"+querys["target"].(string)+"%")
	}

	if querys["created_time"] != nil {
		dbTmp = dbTmp.Where("created_time LIKE ?", "%"+querys["created_time"].(string)+"%")
	}

	if querys["updated_time"] != nil {
		dbTmp = dbTmp.Where("updated_time LIKE ?", "%"+querys["updated_time"].(string)+"%")
	}

	dbTmp.Model(&Assets{}).Count(&count)
	return
}


func DeleteAsset(id int) (bool, int){
	db.Where("id = " + strconv.Itoa(id)).Delete(&Assets{})
	return true, id
}



